home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 388 < prev    next >
Text File  |  1996-08-06  |  3KB  |  64 lines

  1. Path: tko.dec.com!diamond
  2. From: diamond@tko.dec.com (Norman Diamond)
  3. Newsgroups: comp.std.c
  4. Subject: Re: struct pointer compatability...
  5. Date: 15 Feb 1996 02:12:50 GMT
  6. Organization: Digital Equipment Corporation Japan , Tokyo
  7. Message-ID: <4fu4r2$io4@usenet.pa.dec.com>
  8. References: <4fq4sn$kkv@solutions.solon.com>
  9. Reply-To: diamond@jrdv04.enet.dec-j.co.jp (Norman Diamond)
  10. NNTP-Posting-Host: jit533.tko.dec.com
  11.  
  12. In article <4fq4sn$kkv@solutions.solon.com>, seebs@solutions.solon.com (Peter Seebach) writes:
  13. [...]
  14. >Obviously, this is for producing generic linked list code.
  15. >struct a {
  16. >        struct a *next;
  17. >        int i;
  18. >};
  19. >struct b {
  20. >        struct b *next;
  21. >};
  22. >        struct a foo [...] ;
  23. >        [...] (struct b *) &foo [...]
  24. [...]
  25. >Can anyone tell me where this violates the standard, if it does at
  26. >all?  I believe it to be conforming because (I think) pointers to structs
  27. >are compatible as pointers, but I can't find evidence of that anywhere.
  28.  
  29. You should know how to find that pointers to compatible structure types
  30. are compatible, and then you should be able to guess why the standard
  31. didn't pointers to incompatible structure types also be compatible.
  32.  
  33. >I am pretty comfortable with the assertion that this is legal *if*
  34. >(struct a *) and (struct b *) have equivalent representations;
  35.  
  36. Nope.  This would make it possible to cast a (pointer) value of type
  37. (struct a *) to type (struct b *) and cast it back to (struct a *) and
  38. then use it, but not to use the cast value with type (struct b *).
  39. However, if they don't have representations that are either equivalent
  40. or close enough to equivalent, then one of these casts will hit you.
  41.  
  42. >I see no problem with the assertion that the "next" member is in the same
  43. >position in each, I just can't show that it has the same representation.
  44.  
  45. Right.  In addition to that, it is possible for the structure types to
  46. have differing alignment requirements so that most casts will fail.
  47. For example, struct a might have to be aligned on a 15-byte boundary
  48. because it contains a 5-byte pointer, 1-byte pad, and 3-byte int, while
  49. struct b might have to be aligned on a 4-byte boundary because it
  50. contains only a 4-byte pointer.
  51. (Under such an implementation, a pointer to an incomplete struct type can
  52. probably point to any char boundary and therefore requires 8 bytes :-)
  53.  
  54. >FUCK the communications decency act.  Goddamned government.  [literally.]
  55. >The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq - Not A Flying Toy
  56.  
  57. And in other news today, the US government seized machine rtfm.mit.edu.  Next
  58. they'll arrest everyone who talks about a FAQ because of what it sounds like.
  59. --
  60.  <<  If this were the company's opinion, I would not be allowed to post it.  >>
  61. "I paid money for this car, I pay taxes for vehicle registration and a driver's
  62. license, so I can drive in any lane I want, and no innocent victim gets to call
  63. the cops just 'cause the lane's not goin' the same direction as me" - J Spammer
  64.